<?php
namespace app\manage\controller;

use app\manage\controller\Base;
use app\model\ImFriendModel;
use app\model\ImMessageFriendModel;
use think\facade\Db;
use support\View;
use support\Request;
use app\model\UserModel;

class ImFriendController extends Base
{
    public function index(Request $request)
    {
        $user=new UserModel();
        $key=$request->input('key','');
        View::assign('key', $key);
        $order='id desc';
        $user=$user
            ->where('name|phone', 'like', '%'.$key.'%')
            ->order($order)->paginate(['list_rows'=>10,'query'=>request()->all()])->each(function ($item, $key) {
                $shang= Db::name('user')->where('id', $item->fid)->find();
                $item->fname = isset($shang) ? ($shang['name'] ?: $shang['phone']) : '';
                $item->xia = Db::name('user')->where('fid', $item->id)->count('id');
            });
        View::assign('user', $user);
        View::assign('count1', $user->total());
        return view('im_friend/index');
    }
    public function friend(Request $request)
    {
        $key=$request->get('key', '');
        $id=$request->get('id', 0);
        View::assign('key', $key);
        $user=ImFriendModel::alias('if')
            ->join('user u', 'u.id=if.friend_id')
            ->field('if.*,u.name,u.phone,u.img as avatarUrl,u.sex as gender')
            ->where('if.friend_id', $id)
            ->where('u.name|u.phone', 'like', '%'.$key.'%')
            ->order('if.id desc')->paginate(['list_rows'=>10,'query'=>request()->all()]);
        View::assign('user', $user);
        View::assign('count1', $user->total());
        return view('im_friend/friend');
    }
    public function show(Request $request)
    {
        $key=$request->get('key', '');
        View::assign('key', $key);
        $toUserId=$request->get('toUserId', 0);
        View::assign('toUserId', $toUserId);
        $fromUserId=$request->get('fromUserId', 0);
        View::assign('fromUserId', $fromUserId);
        $user=ImMessageFriendModel::alias('m')
            ->leftJoin('im_message_type mt','m.message_type=mt.id')
            ->leftJoin('user u','m.user_id=u.id')
            ->where(function ($query2) use ($toUserId,$fromUserId){
                $query2 ->where(function ($query) use ($toUserId,$fromUserId){
                    $query->where('m.user_id',$toUserId)->where('m.to_user_id',$fromUserId);
                })->whereOr(function ($query) use ($toUserId,$fromUserId){
                    $query->where('m.user_id',$fromUserId)->where('m.to_user_id',$toUserId);
                });
            })
            ->where('m.content', 'like', '%'.$key.'%')
            ->order('m.id desc')
            ->field('m.id,m.content,m.create_time,m.extra_info,m.to_user_id as to_id,m.user_id,mt.name as msgType,u.img as toFace,u.name as toName')
            ->paginate(['list_rows'=>10,'query'=>request()->all()]);

        View::assign('user', $user);
        View::assign('count1', $user->total());
        return view('im_friend/show');
    }
}
